//
// Created by Luhang on 2023/4/26/026.
//

#ifndef INC_0302_VECTOR_H
#define INC_0302_VECTOR_H

#endif //INC_0302_VECTOR_H

#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include "Iterator.h"

using std::cout;
using std::endl;

namespace Lu{
    template<class T>
    class vector{
    public:
        typedef T* iterator;
        typedef const T* const_iterator;

        typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
        typedef ReverseIterator<iterator, const T&, const T*> const_reverse_iterator;

/*        vector()
        :_start(nullptr)
        ,_finish(nullptr)
        ,_end_of_storage(nullptr){
        }

        vector(size_t n, const T& val = T())
            :_start(nullptr)
            ,_finish(nullptr)
            ,_end_of_storage(nullptr){
            reserve(n);
            for (int i = 0; i < n; ++i) {
                push_back(val);
            }
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
            :_start(nullptr)
            ,_finish(nullptr)
            ,_end_of_storage(nullptr){
            while (first != last){
                push_back(*first);
                ++first;
            }
        }
        */
        vector(){
        }

        vector(size_t n, const T& val = T()){
            reserve(n);
            for (size_t i = 0; i < n; ++i) {
                push_back(val);
            }
        }

        vector(int n, const T& val = T()){
            reserve(n);
            for (int i = 0; i < n; ++i) {
                push_back(val);
            }
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last){
            while (first != last){
                push_back(*first);
                ++first;
            }
        }

        ~vector(){
            delete [] _start;
            _start = _finish = _end_of_storage = nullptr;
        }

        //拷贝构造函数：复用其他函数
        /*
        vector(const vector<T>& v){
            reserve(v.capacity());
            for (auto it : v) {
                push_back(it);
            }
        }
        */

        //第一层为深拷贝，但第二层为自定义类型时会浅拷贝
/*
        vector(const vector<T>& v){
            _start = new T[v.capacity()];
            //memcpy浅拷贝，T为自定义类型，不可行
            memcpy(_start, v._start, sizeof(T) * v.size());
            _finish = _start + v.size();
            _end_of_storage = _start + v.capacity();
        }
*/

        //修正后的拷贝构造
/*
        vector(const vector<T>& v){
            _start = new T[v.capacity()];
            for (int i = 0; i < v.size(); ++i) {
                _start[i] = v._start[i];
            }
            _finish = _start + v.size();
            _end_of_storage = _start + v.capacity();
        }
*/
        //拷贝构造：现代写法
        vector(const vector<T>& v){
            vector<T> tmp(v.begin(), v.end());
            swap(tmp);
        }

        void swap(vector<T>& v){
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_end_of_storage, v._end_of_storage);
        }

        //v2 = v1
        vector<T>& operator=(vector<T> v){
            swap(v);
            return *this;
        }

        reverse_iterator rbegin(){
            return reverse_iterator(end());
        }

        reverse_iterator rend(){
            return reverse_iterator(begin());
        }

        iterator begin(){
            return _start;
        }

        iterator end(){
            return _finish;
        }

        const_iterator begin()const{
            return _start;
        }

        const_iterator end()const{
            return _finish;
        }

        //T()是匿名对象调用默认构造
        void resize(size_t n, T val = T()){
            if (n < size())
                _finish = _start + n;
            else{
                if (n > capacity())
                    reserve(n);
                while (_finish != _start + n){
                    *_finish = val;
                    ++_finish;
                }
            }
        }

        void reserve(size_t n){
            if (n > capacity()){
                //先将初始size保存，否则扩容后_finish无法正常更新
                size_t sz = size();

                T* tmp = new T[n];
                if (_start){
                    //浅拷贝，自定义类型扩容时，不可行
                    //memcpy(tmp, _start, sizeof(T) * size());
                    for (int i = 0; i < sz; ++i) {
                        tmp[i] = _start[i];
                    }
                    delete[]_start;
                }
                _start = tmp;
                _finish = _start + sz;
                _end_of_storage = _start + n;
            }
        }

        void push_back(const T& x){
            if (_finish == _end_of_storage)
                reserve(capacity() == 0 ? 4 : 2 * capacity());
            *_finish = x;
            ++_finish;
        }

        void pop_back(){
            assert(!empty());
            --_finish;
        }

        iterator insert(iterator pos, const T& val){
            assert(pos >= _start);
            assert(pos <= _finish);
            if (_finish == _end_of_storage){
                //保存差值方便更新pos
                size_t len = pos - _start;
                reserve(capacity() == 0 ? 4 : 2 * capacity());
                //扩容后更新pos，解决pos(迭代器)失效的问题
                pos = _start + len;
            }
            iterator end = _finish - 1;
            while (end >= pos){
                *(end + 1) = *end;
                --end;
            }
            *pos = val;
            ++_finish;
            return pos;
        }

        iterator erase(iterator pos){
            assert(pos >= _start);
            assert(pos < _finish);
            iterator start = pos + 1;
            while (start != _finish){
                *(start - 1) = *start;
                ++start;
            }
            --_finish;
            return pos;
        }

        T& operator[](size_t pos){
            assert(pos < size());
            return _start[pos];
        }

        const T& operator[](size_t pos)const{
            assert(pos < size());
            return _start[pos];
        }

        size_t capacity()const{
            return _end_of_storage - _start;
        }

        size_t size()const{
            return _finish - _start;
        }

        bool empty()const{
            return _start == _finish;
        }
    private:
        iterator _start = nullptr;
        iterator _finish = nullptr;
        iterator _end_of_storage = nullptr;
    };

    void func(const vector<int>& v){
        for (int i : v) {
            cout << i << " ";
        }
        cout << endl;
    }


    void Test1(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        v1.push_back(5);
        for (int i : v1) {
            cout << i << " ";
        }
        cout << endl;
        func(v1);
        v1.pop_back();
        v1.pop_back();
        for (int i : v1) {
            cout << i << " ";
        }
        cout << endl;
    }

    void Test2(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        v1.push_back(5);
        func(v1);
        v1.resize(10);
        func(v1);
        v1.resize(3);
        func(v1);
        v1.resize(5,6);
        func(v1);
    }

    //迭代器失效——insert
    void Test3(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        //v1.push_back(5);
        func(v1);
        v1.insert(v1.begin(),0);
        func(v1);
        auto pos = std::find(v1.begin(),v1.end(),3);
        if (pos != v1.end())
            v1.insert(pos,66);
        func(v1);
        (*pos)++;
        func(v1);
    }

    //测新insert
    void Test4(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        func(v1);
        auto pos = std::find(v1.begin(),v1.end(),3);
        if (pos != v1.end())
            //v1.insert(pos,66);
            pos = v1.insert(pos,66);
        func(v1);
        //insert后最好不要访问pos
        (*pos)++;
        func(v1);
    }

    //迭代器失效——erase
    void Test5(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        func(v1);
        auto pos = std::find(v1.begin(),v1.end(),2);
        if (pos != v1.end())
            v1.erase(pos);
        func(v1);
        (*pos)++;
        func(v1);
    }

    //库中erase
    void Test6(){
        std::vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        for (auto e : v1) {
            cout << e << " ";
        }
        cout << endl;
        auto it = v1.begin();
        while (it != v1.end()){
            if (*it % 2 == 0)
                v1.erase(it);
            ++it;
        }
        for (auto e : v1) {
            cout << e << " ";
        }
        cout << endl;
    }

    //测新erase
    void Test7(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        v1.push_back(10);
        for (auto e : v1) {
            cout << e << " ";
        }
        cout << endl;
        auto it = v1.begin();
        while (it != v1.end()){
            if (*it % 2 == 0)
                it = v1.erase(it);
            else
                ++it;
        }
        for (auto e : v1) {
            cout << e << " ";
        }
        cout << endl;
    }

    //测构造函数
    void Test8(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        v1.push_back(10);
        func(v1);
        vector<int> v2(v1.begin(), v1.end()-1);
        func(v2);
        vector<int> v3((size_t)5,9);
        func(v3);
    }

    //测构造函数
    void Test9(){
        std::string s1 = "hello chongqing";
        vector<char> v1(s1.begin(), s1.end());
        for (auto it : v1) {
            cout << it;
        }
        cout << endl;
        int a[] = {10,20,30};
        vector<int> v2(a,a+3);
        for (auto it : v2) {
            cout << it << " ";
        }
        cout << endl;
        vector<int> v3(10,9);
        for (auto it : v3) {
            cout << it << " ";
        }
    }

    //algorithm中的sort（默认升序）
    void Test10(){
        int a[] = {20,10,30};
        vector<int> v1(a,a+3);
        v1.push_back(6);
        for (auto it : v1) {
            cout << it << " ";
        }
        cout << endl;
        std::sort(v1.begin(), v1.end());
        for (auto it : v1) {
            cout << it << " ";
        }
        cout << endl;
        //降序：传一个仿函数
        std::greater<int> g;
        std::sort(v1.begin(), v1.end(), g);
        for (auto it : v1) {
            cout << it << " ";
        }
        cout << endl;
        //直接传一个匿名对象
        std::sort(v1.begin(), v1.end(), std::greater<int>());
        for (auto it : v1) {
            cout << it << " ";
        }
    }

    //拷贝构造
    void Test11(){
        vector<int> vi1(3,9);
        func(vi1);
        vector<int> vi2(vi1);
        func(vi2);

        vector<std::string> vs1(4,"hello");
        for (const auto& it : vs1) {
            cout << it << " ";
        }
        cout << endl;
        vector<std::string> vs2(vs1);
        for (const auto& it : vs2) {
            cout << it << " ";
        }
        cout << endl;
    }

    //涉及扩容时的深浅拷贝问题
    void Test12(){
        vector<std::string> vs1(4,"hello");
        for (const auto& it : vs1) {
            cout << it << " ";
        }
        cout << endl;
        vs1.push_back("chongqing");
        for (const auto& it : vs1) {
            cout << it << " ";
        }
        cout << endl;
    }

    //<T>中自定义类型为vector时
    class Solution {
    public:
        static vector<vector<int>> generate(int numRows) {
            vector<vector<int>> vv;
            vv.resize(numRows, vector<int>());
            for (size_t i = 0; i < vv.size(); ++i)
            {
                vv[i].resize(i + 1, 0);
                vv[i][0] = vv[i][vv[i].size() - 1] = 1;
            }
            for (size_t i = 0; i < vv.size(); ++i)
            {
                for (size_t j = 0; j < vv[i].size(); ++j)
                {
                    if (vv[i][j] == 0)
                        vv[i][j] = vv[i - 1][j] + vv[i - 1][j - 1];
                }
            }
            return vv;
        }
    };
    void Test13(){
        vector<vector<int>> ret = Solution().generate(6);
        for (auto & i : ret){
            for (int j : i)
                cout << j << " ";
            cout << endl;
        }
        cout << endl;
        vector<vector<int>> vv(ret);
        for (auto & i : vv){
            for (int j : i)
                cout << j << " ";
            cout << endl;
        }
        cout << endl;
    }

    void Test14(){
        vector<int> v1;
        v1.push_back(1);
        v1.push_back(2);
        v1.push_back(3);
        v1.push_back(4);
        v1.push_back(10);
        for (auto e : v1) {
            cout << e << " ";
        }
        cout << endl;
        auto it = v1.rbegin();
        while (it != v1.rend()){
            cout << *it << " ";
            ++it;
        }
        cout << endl;
    }
}